#include "../../../shared/dal/data_cache.hpp"
#include "logger.hpp"
#include <gflags/gflags.h>
#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include <thread>

DEFINE_bool(run_mode, false, "程序的运行模式: false-调试 true-发布");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(ip, "127.0.0.1", "这是服务器的IP地址, 格式: 127.0.0.1");
DEFINE_int32(port, 6379, "这是redis服务的端口, 格式:8080");
DEFINE_int32(db, 0, "库的编号: 默认0号");
DEFINE_bool(keep_alive, true, "是否进行长连接保活");

std::shared_ptr<sw::redis::Redis> client;

TEST(session_test, fun_test)
{
    shared::dal::Session session(client);
    session.append("session-1", "userID-1");
    session.append("session-2", "userID-2");
    session.append("session-3", "userID-3");
    session.append("session-4", "userID-4");

    session.remove("session-2");
    session.remove("session-3");

    ASSERT_EQ(session.getUserId("session-1"), "userID-1");
    ASSERT_FALSE(session.getUserId("session-2"));
    ASSERT_FALSE(session.getUserId("session-3"));
    ASSERT_EQ(session.getUserId("session-4"), "userID-4");
}

TEST(status_test, fun_test)
{
    shared::dal::Status status(client);
    status.append("userID-1");
    status.append("userID-2");
    status.append("userID-3");

    status.remove("userID-3");

    ASSERT_TRUE(status.exists("userID-1"));
    ASSERT_TRUE(status.exists("userID-2"));
    ASSERT_FALSE(status.exists("userID-3"));
}

TEST(verifyCode_test, fun_test)
{
    shared::dal::VerifyCode codes(client);
    codes.append("codeID-1", "code-1");
    codes.append("codeID-2", "code-2");
    codes.append("codeID-3", "code-3");

    codes.remove("codeID-1");

    ASSERT_FALSE(codes.getVerifyCode("codeID-1"));
    ASSERT_EQ(codes.getVerifyCode("codeID-2"), "code-2");
    ASSERT_EQ(codes.getVerifyCode("codeID-3"), "code-3");

    // 等待保活时间到期
    std::this_thread::sleep_for(std::chrono::seconds(4));

    ASSERT_FALSE(codes.getVerifyCode("codeID-1"));
    ASSERT_FALSE(codes.getVerifyCode("codeID-2"));
    ASSERT_FALSE(codes.getVerifyCode("codeID-3"));
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    shared::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    client = shared::dal::RedisClientFactory::create(FLAGS_ip, FLAGS_port, FLAGS_db, FLAGS_keep_alive);

    return RUN_ALL_TESTS();
}